package logic

import (
	"context"
	"userrpc/internal/util"

	"userrpc/internal/svc"
	"userrpc/pb/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MobileLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MobileLogic {
	return &MobileLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MobileLogic) Mobile(in *userrpc.MobileRequest) (*userrpc.MobileResponse, error) {
	// todo: add your logic here and delete this line
	code, err := util.Send(in.Mobile)
	if err != nil {
		return &userrpc.MobileResponse{
			Code: 400,
			Msg:  err.Error(),
			Data: "",
		}, nil
	}
	mobile := in.Mobile
	SendCount := "sms:send_count" + mobile
	SendTime := "sms:send_time" + mobile
	err = util.Count(SendCount, SendTime)
	if err != nil {
		return &userrpc.MobileResponse{
			Code: 400,
			Msg:  err.Error(),
			Data: "",
		}, nil
	}
	util.SetRedis(SendCount, SendTime)
	return &userrpc.MobileResponse{
		Code: 200,
		Msg:  "发送短信成功",
		Data: code,
	}, nil
}
